package com.jj.core.rpc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Copyrights: QidianGroup@JJ
 *
 * @author: LuHan
 * @date: 2018/1/15, 13:56
 */
@Component
public class UserCallManager {
    private static Logger logger = LoggerFactory.getLogger(UserCallManager.class);

    private Map<Integer, ParameterConvert> convertMap = new HashMap<>();

    public void registHandler(Integer msgId, ParameterConvert convert){
        convertMap.put(msgId, convert);
    }

    public Object[] getParameters(Integer msgId, byte[] payload){
        ParameterConvert convert = convertMap.get(msgId);
        Object[] ret = null;
        if(convert != null){
            ret = convert.convert(payload);
            logger.debug("convert parameter:{}", ret);
        }else{
            logger.warn("Convert not found for {}", msgId);
        }
        return ret;
    }
}
